package com.whzykj.common;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

public class CommonUtils {
	/** 默认的格式化方式 */
	private static final String defaultFormat = "yyyy-MM-dd HH:mm:ss";

	public static String getDate() {
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		Date currentDate = new Date();
		String formatCurrentDate = dateFormat.format(currentDate).toString();

		return formatCurrentDate;
	}
	
	public static String getDateStr(Date currentDate) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String formatCurrentDate = dateFormat.format(currentDate).toString();
		return formatCurrentDate;
	}
	
	public static String getDateToYMDStr(Date currentDate) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		String formatCurrentDate = dateFormat.format(currentDate).toString();
		return formatCurrentDate;
	}

	public static String getCurrentDate() {
		String format = "yyyy-MM-dd";
		Date date = new Date();
		date.setTime(System.currentTimeMillis());
		if (format == null || "".equals(format.trim())) {
			format = defaultFormat;
		}
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(date);
	}

	public static String getCurrentTime() {
		String format = "yyyyMMddHHmmss";
		Date date = new Date();
		date.setTime(System.currentTimeMillis());
		if (format == null || "".equals(format.trim())) {
			format = defaultFormat;
		}
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(date);
	}
	
	/** 
     * 获得指定日期的后一天 
     *  
     * @param specifiedDay 
     * @return 
     */  
    public static String getSpecifiedDayAfter(String specifiedDay) {  
        Calendar c = Calendar.getInstance();  
        Date date = null;  
        try {  
            date = new SimpleDateFormat("yyyy-MM-dd").parse(specifiedDay);  
        } catch (ParseException e) {  
            e.printStackTrace();  
        }  
        c.setTime(date);  
        int day = c.get(Calendar.DATE);  
        c.set(Calendar.DATE, day + 1);
        String dayAfter = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());  
        return dayAfter;  
    }
    
    static public Date strparseDate(String s)  {
		if(s == null || "".equals(s)){
			return null;
		}
		SimpleDateFormat format = new SimpleDateFormat(defaultFormat);
		try {
			return format.parse(s);
		} catch (ParseException e) {
		}
		return null ;
	}
    
    static public Date parseDate(String s)  {
		if(s == null || "".equals(s)){
			return null;
		}
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		try {
			return format.parse(s);
		} catch (ParseException e) {
		}
		return null ;
	}
	
	/**
	 * 获得指定长度的随机码
	 * @param len
	 * @return
	 */
	public static String getRandomCode(int len){
		StringBuffer rntStr=new StringBuffer();
		Random r=new Random();
		for(int i=0;i<len;i++){
			rntStr.append(r.nextInt(10));
		}
		return rntStr.toString();
	}
	
	public static String getMapKeyVal(Map<String, Object> map, String key) {
		if (map == null)
			return "";
		if (key == null)
			return "";
		Object obj = map.get(key);
		String result;
		if (obj == null) {
			result = "";
		} else {
			result = obj.toString();
		}
		return result;
	}
	
	/**
	 * 生成订单号
	 * @param len
	 * @return
	 */
	public static String generateSeq(){
		String id="APP"+CommonUtils.getCurrentTime()+CommonUtils.getRandomCode(3);
		return id;
	}
	
	public static String generateAcctSeq(){
		String id="ZJ"+CommonUtils.getCurrentTime()+CommonUtils.getRandomCode(6);
		return id;
	}
	
	public static String generatePayBillSeq(){
		String id="ZF"+CommonUtils.getCurrentTime()+CommonUtils.getRandomCode(6);
		return id;
	}
	
	public static String generateRechargeBill(){
		String id="CZ"+CommonUtils.getCurrentTime()+CommonUtils.getRandomCode(6);
		return id;
	}
	
	public static String generateVirtualOrder(){
		String id="VR"+CommonUtils.getCurrentTime()+CommonUtils.getRandomCode(6);
		return id;
	}

	public static boolean isMobileNO(String mobiles){  
        //Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");
		Pattern p = Pattern.compile("^[1-9]\\d{10}$");
		Matcher m = p.matcher(mobiles);  
		return m.matches();  
    }
	
	public static void main(String[] args) {
		System.out.println(CommonUtils.count("79","39"));
		System.out.println(new Date());
	}
	
	static public Date parseTime(String s) throws ParseException {
		SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
		return format.parse(s);
	}
	
	public static String dateForString(Date date, String format) {
		String dateTime = "";
		if (date == null) {
			return dateTime;
		}
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		dateTime = formatter.format(date);
		return dateTime;
	}
	
	/**
	 * 计算市斤价格
	 * @param len
	 * @return
	 */
	public static BigDecimal count(String saleprice, String weight) {
		BigDecimal sale = new BigDecimal(saleprice);
		BigDecimal weig = new BigDecimal(weight);
		return sale.divide(weig, 2, BigDecimal.ROUND_HALF_UP);
	}
	
	public static BigDecimal formatComma2BigDecimal(Object obj) {  
        String val = String.valueOf(obj);  
        if (val == null)  
            return new BigDecimal("0.00");  
  
        val = val.replaceAll(",", "");  
        if (!isNumber(val))  
            return new BigDecimal("0.00");  
  
        BigDecimal decimal = new BigDecimal(val);  
        decimal = decimal.setScale(2, RoundingMode.HALF_UP);  
  
        return decimal;  
  
    }  
	
	public static String formatCommaAnd2Point(Object obj) {
		BigDecimal decimal = formatComma2BigDecimal(obj);
		DecimalFormat df = new DecimalFormat("#,###.00");
		String decimalStr = df.format(decimal).equals(".00") ? "0.00" : df
				.format(decimal);
		if (decimalStr.startsWith(".")) {
			decimalStr = "0" + decimalStr;
		}
		return decimalStr;
	}

	public static boolean isDouble(String value) {
		try {
			Double.parseDouble(value);
			if (value.contains("."))
				return true;
			return false;
		} catch (NumberFormatException e) {
			return false;
		}
	}

	public static boolean isInteger(String value) {
		try {
			Integer.parseInt(value);
			return true;
		} catch (NumberFormatException e) {
			return false;
		}
	}

	public static boolean isNumber(String value) {
		return isInteger(value) || isDouble(value);
	}
	
	/**
	* 获取ip地址
	* @param request
	* @return
	*/
	public static String getIpAddr(HttpServletRequest request) {
		 try        {           
			 String ip = InetAddress.getLocalHost().getHostAddress();   
			return ip;
			 } 
		 catch (UnknownHostException e)  {
			return null; 
		 }
	}
}
